package main

import (
	"bytes"
	"encoding/csv"
	"fmt"
	"math/rand"
	"os"
	"strings"
	"time"
)

const (
	dataSource = "./data/heroes.csv"
)

func main() {
	heroesData, err := os.ReadFile(dataSource)
	if err != nil {
		panic(err)
	}

	csvReader := csv.NewReader(bytes.NewReader(heroesData))
	records, err := csvReader.ReadAll()
	if err != nil {
		panic(err)
	}

	heroes := make([]string, 0, len(records))
	for _, record := range records {
		if record[2] == "true" {
			heroes = append(heroes, record[1])
		}
	}

	for i := 0; i < 6; i++ {
		gamesHeroes := genGamesHeroes(heroes)
		printGamesHeroes(gamesHeroes)
	}
}

func genGamesHeroes(heroes []string) [][]string {
	rand.Seed(time.Now().UnixNano())
	rand.Shuffle(len(heroes), func(i, j int) {
		heroes[i], heroes[j] = heroes[j], heroes[i]
	})
	gamesHeroes := make([][]string, 0, 5)
	for i := 0; i < 5; i++ {
		games := make([]string, 0, 5)
		for j := 0; j < 5; j++ {
			start := i*15 + j*3
			games = append(games, strings.Join(heroes[start:start+3], ","))
		}
		gamesHeroes = append(gamesHeroes, games)
	}

	return gamesHeroes
}

func printGamesHeroes(gamesHeroes [][]string) {
	fmt.Println("\tTOP\t\tJUG\t\tMID\t\tADC\t\tSUP")
	for game, heroes := range gamesHeroes {
		fmt.Printf("GAME %d\t", game+1)
		for _, hero := range heroes {
			fmt.Printf("%s\t\t", hero)
		}
		fmt.Println()
	}
}
